<?php
namespace Home\Controller;

class XlwController extends HomeController{
    
    public function index(){
        $M_product=M('product');
        $M_product_image=M('product_image');
        
        $gift_id = I('giftid'); // 选礼物分类编号
        $category_id=I('cid');
        $page=I('page',1);
        $page<=0 && $page=1;
        $pagesize=12;
        
        $type='putong';
        
        $map=array();
        $options = array();
        $model = M('product');
        
        $prefix = C('DB_PREFIX');
        $l_table = $prefix.('product');
        $m_table = $prefix.('product_special_gift');
        $r_table = $prefix.('special_gift');

        $model = $model->table($l_table.' l')->join($m_table.' m ON m.product_id=l.id')->join($r_table.' r ON r.id = m.special_gift_id');
        
        $options['field'] = array('l.id', 'l.title', 'l.price', 'l.quantity');
        $options['group'] = 'l.id';
        $options['order'] = 'l.id DESC';
        $map['l.is_show'] = 1;
        $map['l.status'] = 1;
        $map['l.type'] = $type;
        $map['l.in_special_gift'] = 1;  
        

        $M_product_category=M('product_category');
        $cid=I('request.cid');
        $fid=I('request.fid');
        
        if($cid){
            $map['l.category_id']=$cid;
        }
        
        else if($fid){
            $cids=$M_product_category->where(array('pid'=>$fid))->field('id')->select();
            $cids=arr2tostr($cids);
            $map['l.category_id']=array('in',$cids);
        }        
       
        $gift_id &&  $map['m.special_gift_id'] = $gift_id;
        !(empty($category_id) || !is_numeric($category_id)) && $map['l.category_id'] = array('in',$category_id);
       
        $options['where'] = $map;
        
        $OPT = new \ReflectionProperty($model,'options');
        $OPT->setAccessible(true);
         
        $options = array_merge( (array)$OPT->getValue($model), $options );
        $total = $model->where($options['where'])->count();
        
        if ($page > 0 && $pagesize > 0) {
            $start = ($page - 1) * $pagesize;
            $options['limit'] = $start.','.$pagesize;
        }
        
        $model->setProperty('options',$options);
         
        $product_list = $model->select(); 
        foreach ($product_list as &$item) {
            $image_info = $M_product_image->where(array('product_id'=>$item['id']))->order('id ASC')->find();
            $item['image'] = get_cover($image_info['cover_id'],'path');
        }      
        $this->assign('p_list',$product_list);
        
        //广告
        $ad_positon=M('ad_position')->where(array('identifier'=>'ad_homepagetop'))->find();
        
        $adlist=M('ad')->where(array('position_id'=>$ad_positon['id']))
        ->order('sort DESC')->field('cover_id,title,link,product_id')->select();
        foreach($adlist as &$item){
            $item['image']=get_cover($item['cover_id'],'path');
        }
        
        $this->assign('adlist',$adlist);
        $this->display();
    }
}